#ifndef MENU_H
#define MENU_H

#include "BaseEntity.h"
#include "qjsonobject.h"
#include <QJsonArray>
#include <QString>
#include <QStringList>


class Menu : public BaseEntity{
public:
    qulonglong menuId;
    QString menuName;
    QString parentName;
    qulonglong parentId;
    int orderNum;
    QString path;
    QString component;
    QString query;
    QString isFrame;
    QString isCache;
    QString menuType;
    QString visible;
    QString status;
    QString perms;
    QString icon;
    QList<Menu> children;

    static Menu toMenu(const QJsonObject& obj) {
        Menu menu;
        menu.menuId = static_cast<qulonglong>(obj["menuId"].toVariant().toLongLong());
        menu.menuName = obj["menuName"].toString();
        menu.parentName = obj["parentName"].toString();
        menu.parentId = static_cast<qulonglong>(obj["parentId"].toVariant().toLongLong());
        menu.orderNum = obj["orderNum"].toInt();
        menu.path = obj["path"].toString();
        menu.component = obj["component"].toString();
        menu.query = obj["query"].toString();
        menu.isFrame = obj["isFrame"].toString();
        menu.isCache = obj["isCache"].toString();
        menu.menuType = obj["menuType"].toString();
        menu.visible = obj["visible"].toString();
        menu.status = obj["status"].toString();
        menu.perms = obj["perms"].toString();
        menu.icon = obj["icon"].toString();
        menu.searchValue = obj["searchValue"].toString();
        menu.createBy = obj["createBy"].toString();
        menu.createTime = obj["createTime"].toString();
        menu.updateBy = obj["updateBy"].toString();
        menu.updateTime = obj["updateTime"].toString();
        menu.remark = obj["remark"].toString();

        QJsonArray childrenArray = obj["children"].toArray();
        for (const QJsonValue& childVal : childrenArray) {
            if (childVal.isObject()) {
                QJsonObject childObj = childVal.toObject();
                Menu childMenu = toMenu(childObj);
                menu.children.append(childMenu);
            }
        }
        return menu;
    }

    QJsonObject toJson() const {
        QJsonObject json;
        json["menuId"] = static_cast<qint64>(menuId);
        json["menuName"] = menuName;
        json["parentName"] = parentName;
        json["parentId"] = static_cast<qint64>(parentId);
        json["orderNum"] = orderNum;
        json["path"] = path;
        json["component"] = component;
        json["query"] = query;
        json["isFrame"] = isFrame;
        json["isCache"] = isCache;
        json["menuType"] = menuType;
        json["visible"] = visible;
        json["status"] = status;
        json["perms"] = perms;
        json["icon"] = icon;
        json["remark"] = remark;
        json["updateTime"] = updateTime;
        json["updateBy"] = updateBy;
        json["createTime"] = createTime;
        json["createBy"] = createBy;
        json["searchValue"] = searchValue;


        QJsonArray childrenArray;
        for (const Menu& child : children) {
            childrenArray.append(child.toJson());
        }
        json["children"] = childrenArray;

        return json;
    }
};

#endif // MENU_H
